#!/bin/sh
# - test

## Automated hostapd/wpa_supplicant testing with mac80211_hwsim.
## See: https://github.com/jmalinen/hostap/tree/master/tests/hwsim

. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/reproduce-log.sh

cd $BENCHMARK_ROOT/hwsim/tests/hwsim || die "no $BENCHMARK_ROOT/hwsim/tests/hwsim"

test_file=$BENCHMARK_ROOT/hwsim/hwsim-$test
[ -f "$test_file" ] || die "can not find $test_file under $BENCHMARK_ROOT/hwsim"

nm_conf="/etc/NetworkManager/NetworkManager.conf"
[ -f "$nm_conf" ] && log_eval "cat >> $nm_conf <<EOF
[keyfile]
unmanaged-devices=mac:02:00:00:00:00:00;mac:02:00:00:00:01:00;mac:02:00:00:00:02:00;mac:02:00:00:00:03:00;mac:02:00:00:00:04:00
EOF"

[ $USER ] || log_eval export USER=root

is_excluded()
{
	local testcase=$1
	local exclude_file=$BENCHMARK_ROOT/hwsim/hwsim-exclude

	[ -f "$exclude_file" ] && grep -q -w $testcase "$exclude_file" && return 0

	return 1
}

log_cmd ./build.sh 2>&1 || die "fail to build test environment"


run_tests_opt=
start_opt=

case "$test" in
long-param-*)
	run_tests_opt="--long"
	;;
mcc)
	start_opt="channels=4"
	;;
esac

# longer sleep time in line 166 to 0.5
# vi lkp/benchmarks/hwsim/tests/hwsim/start.sh
#162 for i in 0 1 2 3 4 5 6 7 8 9; do
#163     if [ -e /tmp/wpas-wlan0 ]; then
#164         break
#165     fi
#166     sleep 0.05
#167 done
sed -i 's/0.05/0.5/g' start.sh
log_cmd ./start.sh $start_opt || {
	echo "fail to setup test environment" 1>&2
	exit 1
}

while read testcase
do
	is_excluded $testcase && echo "LKP SKIP $testcase" && continue

	log_cmd ./run-tests.py $testcase $run_tests_opt 2>&1
done < $test_file

log_cmd ./stop.sh
